package com.danger.Activity.Account;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.danger.Activity.BackHandledFragment;
import com.danger.Activity.R;
import com.danger.Model.bean.UserInfoBean;
import com.danger.Presenter.BindMobilePresenter;
import com.danger.Presenter.BindMobilePresenterImpl;
import com.danger.View.BindMobileView;

/**
 * Created by danger on 2016/4/24.
 */
public class BindMobileFragment extends BackHandledFragment implements BindMobileView, View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private EditText bind_phone_editText;
    private EditText bind_check_number_editText;
    private Button send_check_number_button;
    private Button finish_register_button;
    private Button cancel_register_button;
    private Toolbar toolbar;
    private ProgressDialog dialog;
    private BindMobilePresenter bindMobilePresenter;
    private AccountActivity activity;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_bind_mobile, container, false);

        initView();
        initEvent();

        return view;
    }

    private void initView() {
        activity = (AccountActivity) getActivity();
        toolbar = activity.getToolbar();
        toolbar.setTitle("");
        activity.getToolbarTitle().setText("绑定电话");
        toolbar.setNavigationIcon(R.mipmap.back);
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        bindMobilePresenter = new BindMobilePresenterImpl(activity, this);
        dialog = new ProgressDialog(activity);
        dialog.setMessage("正在注册...");
        bind_phone_editText = (EditText) view.findViewById(R.id.bind_phone_editText);
        bind_check_number_editText = (EditText) view.findViewById(R.id.bind_check_number_editText);
        send_check_number_button = (Button) view.findViewById(R.id.send_check_number_button);
        finish_register_button = (Button) view.findViewById(R.id.finish_register_button);
        cancel_register_button = (Button) view.findViewById(R.id.cancel_register_button);
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        send_check_number_button.setOnClickListener(this);
        finish_register_button.setOnClickListener(this);
        cancel_register_button.setOnClickListener(this);
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String msg) {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public UserInfoBean getUserInfo() {
        UserInfoBean userInfo = new UserInfoBean();
        Bundle bundle = getArguments();
        if (bundle != null) {
            userInfo.setUsername(bundle.getString("username"));
            userInfo.setPassword(bundle.getString("password"));
            userInfo.setQq(bundle.getString("qq"));
            userInfo.setPhoto(bundle.getString("photo"));
            userInfo.setEmail(bundle.getString("email"));
        }
        return userInfo;
    }

    @Override
    public void startTimer() {

    }

    @Override
    public void cancelTimer() {

    }

    @Override
    public Button getSendButton() {
        return send_check_number_button;
    }

    @Override
    public void toLogin() {
        getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.account_container, new LoginFragment()).commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_check_number_button:
                bindMobilePresenter.sendCode(bind_phone_editText.getText().toString());
                break;
            case R.id.finish_register_button:
                bindMobilePresenter.register(bind_phone_editText.getText().toString(), bind_check_number_editText.getText().toString());
                break;
            case R.id.cancel_register_button:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.account_container, new LoginFragment()).commit();
            default:
                break;
        }
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }
}
